package com.mymall.serve.config;

import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 跨域配置类
 *
 * @author weiwei
 * @since 2020/4/11 16:48
 */
@Configuration
public class CorsConfig {


    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();

        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.setAllowedMethods(Lists.newArrayList(CorsConfiguration.ALL));
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.setMaxAge(24 * 3600L);

        // 配置所有请求
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }
}